package com.unicom.office.parser;

import com.unicom.office.config.OfficeConfig;
import com.unicom.office.func.FuncBase;
import com.unicom.office.node.*;
import com.unicom.office.runtime.OfficeRuntime;

public abstract class OfficeParser {
    protected OfficeConfig config;
    protected OfficeRuntime runtime;

    public OfficeParser(OfficeConfig config, OfficeRuntime runtime){
        this.config = config;
        this.runtime = runtime;
    }

    public FuncBase parse(String sourcePath){
        Object source = getSource(sourcePath);
        OfficeNode parsed = parse(source);
        if(parsed.getParent() != null){
            return parsed.getParent();
        }else {
            return parsed;
        }
    }

    protected OfficeNode parse(Object source){
        runtime.beforeParse();
        OfficeNode officeNode = parse(source, null);
        runtime.afterParse();
        return officeNode;
    }

    public void setConfig(OfficeConfig config) {
        this.config = config;
    }

    public void setRuntime(OfficeRuntime runtime) {
        this.runtime = runtime;
    }

    public abstract OfficeNode parse(Object source, OfficeNode parentNode);
    public abstract Object getSource(String sourcePath);
    protected abstract OfficeNodeType parseType(Object source);

    protected abstract TextboxNode parseTextbox(Object source, OfficeNode parentNode);
    protected abstract RowNode parseRow(Object source, OfficeNode parentNode);
    protected abstract CellNode parseCell(Object source, OfficeNode parentNode);
    protected abstract ParagraphNode parseParagraph(Object source, OfficeNode parentNode);
    protected abstract TextRangeNode parseTextRange(Object source, OfficeNode parentNode);
    protected abstract PictureNode parsePicture(Object source, OfficeNode parentNode);
}
